package com.zds.support.extensions

import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.zds.support.base.SupportBaseActivity

inline fun View.setMargin(left: Int, top: Int, right: Int, bottom: Int) {
    val layoutParams = this.layoutParams
    if (layoutParams is ViewGroup.MarginLayoutParams) {
        layoutParams.setMargins(left, top, right, bottom)
        this.layoutParams = layoutParams
    } else if (layoutParams is RelativeLayout.LayoutParams) {
        layoutParams.setMargins(left, top, right, bottom)
        this.layoutParams = layoutParams
    } else if (layoutParams is LinearLayout.LayoutParams) {
        layoutParams.setMargins(left, top, right, bottom)
        this.layoutParams = layoutParams
    } else {
    }
}
inline fun View.setHeight( height: Int) {
    val layoutParams = this.layoutParams
    layoutParams.height = height
    this.layoutParams = layoutParams
}

inline fun View.setWidthAndHeight(width: Int, height: Int) {
    val layoutParams = this.layoutParams
    if (layoutParams is ViewGroup.MarginLayoutParams) {
        layoutParams.width = width
        layoutParams.height = height
        this.layoutParams = layoutParams
    } else if (layoutParams is RelativeLayout.LayoutParams) {
        layoutParams.width = width
        layoutParams.height = height
        this.layoutParams = layoutParams
    } else if (layoutParams is LinearLayout.LayoutParams) {
        layoutParams.width = width
        layoutParams.height = height
        this.layoutParams = layoutParams
    } else {
    }
}


inline fun View.visible() {
    visibility = View.VISIBLE
}

inline fun View.visibleOrGone(isVisible: Boolean) {
    if (isVisible) {
        this.visibility = View.VISIBLE
    } else {
        this.visibility = View.GONE
    }
}

inline fun View.gone() {
    visibility = View.GONE
}

inline fun View.inVisible() {
    visibility = View.INVISIBLE
}


inline fun View.OnClickListener.addView(vararg views: View) {
    for (view in views) {
        view.setOnClickListener(this)
    }
}

fun EditText.checkBlank(msg:String,activity:SupportBaseActivity):String?{
    val text = text.toString()
    if(text.isBlank()){
        activity?.toast(msg)
        return null
    }
    return text
}